package cn.niit.letuseatdemo.service.impl;

import cn.niit.letuseatdemo.entity.domain.Store;
import cn.niit.letuseatdemo.entity.domain.SysUser;
import cn.niit.letuseatdemo.mapper.StoreMapper;
import cn.niit.letuseatdemo.service.StoreService;
import cn.niit.letuseatdemo.util.LoginStoreUtil;
import cn.niit.letuseatdemo.util.LoginUserUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 商家接收实现类
 * @Author: su_fuming
 * @CreateTime： 2022/11/2 19:17
 */
@Mapper
@Service
public class StoreServiceImpl implements StoreService
{
    @Autowired
    private StoreMapper storeMapper;

    /*
      * @Description: (商家登录)根据账号、密码获取对应的用户对象
      * @Author: zhang_bowen
      * @CreateTime: 2022/11/30  22:49
 * @return: cn.niit.letuseatdemo.entity.domain.Store
      **/
    @Override
    public Store getStoreByStorePhoneStorePassword(String storePhone,String storePassword){
        return storeMapper.selectStoreByStorePhoneStorePassword(storePhone,storePassword);
    }

    /**
     * 根据 storeId 查询对应的商家对象
     *
     * @param storeId 商家主键
     * @return 商家
     */
    @Override
    public Store selectStoreByStoreId(Long storeId)
    {
        return storeMapper.selectStoreByStoreId(storeId);
    }

    /**
     * 查询商家列表
     *
     * @param store 商家
     * @return 商家
     */
    @Override
    public List<Store> selectStoreList(Store store){
        /**
         *  开启分页查询
         *  PageHelper.startPage(goods.getPage(),goods.getRows())
         *  1.将改代码放在要执行分页查询语句之前
         *   2.两个参数，分别是当前第几页，每页记录数
         **/
        if(store.getPage() == null){
            store.setPage(1);
        }
        PageHelper.startPage(store.getPage(),store.getRows());
        List<Store> storeList = storeMapper.selectStoreList(store);
        return storeList;
    }

    /*
    * @Description:（用户侧）查商家列表
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/7 19:05
     * @param: store
     * @return: java.util.List<cn.niit.letuseatdemo.entity.domain.Store>
    **/
    @Override
    public List<Store> selectAllStoreList(Store store) {
        return storeMapper.selectAdminStoreList(store);
    }

    /**
     * 根据 categoryId 查询对应的商家列表
     *
     * @param categoryId 类别id
     * @return 商家列表
     */
    @Override
    public List<Store> selectStoreListByCategoryId(Long categoryId){
        //返回使用方法的查找内容
        return storeMapper.selectStoreListByCategoryId(categoryId);
    }

    /**
     * 新增商家
     *
     * @param store 商家
     * @return 结果
     */
    @Override
    public int insertStore(Store store)
    {
        /** 为delFlag 赋予默认值*/
        store.setDelFlag(0);
        return storeMapper.insertStore(store);
    }
    /**
     * 修改商家
     *
     * @param store 商家
     * @return 结果
     */
    @Override
    public int updateStore(Store store)
    {
        Store loginStore = LoginStoreUtil.getLoginStore();
        store.setUpdateUserId(loginStore.getStoreId());
        store.setUpdateUserName(loginStore.getStoreName());
        store.setGmtUpdate(new Date());
        return storeMapper.updateStore(store);
    }

    /**
     * 批量删除商家
     *
     * @param storeIds 需要删除的商家主键
     * @return 结果
     */
    @Override
    public int deleteStoreByStoreIds(String storeIds)
    {
        return 0;
    }
    /**
     * 删除商家信息
     *
     * @param storeId 商家主键
     * @return 结果
     */
    @Override
    public int deleteStoreByStoreId(Long storeId)
    {
        return storeMapper.deleteStoreByStoreId(storeId);
    }

    /*
      * @Description: (管理员)根据商家ID查询商家
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/1  11:18
 * @return: cn.niit.letuseatdemo.entity.domain.Store
      **/
    @Override
    public Store selectAdminStoreByStoreId(Long storeId) {

        return storeMapper.selectAdminStoreByStoreId(storeId);
    }

    /*
      * @Description: (管理员)查询商家列表
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/1  11:18
 * @return: java.util.List<cn.niit.letuseatdemo.entity.domain.Store>
      **/
    @Override
    public List<Store> selectAdminStoreList(Store store) {
        //开启分页查询
        PageHelper.startPage(store.getPage(),store.getRows());
        List<Store> storeAdminList = storeMapper.selectAdminStoreList(store);
        return storeAdminList;
    }

    /*
      * @Description:（管理员）修改商家入驻审核状态
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/1  11:18
 * @return: int
      **/
    @Override
    public int updateAdminStore(Store store) {
        return storeMapper.updateAdminStore(store);
    }

    /*
      * @Description: 商家入驻
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/1  22:39
 * @return: null
      **/
    @Override
   public int insertAdminStore(Store store){
//        /** 为delFlag 赋予默认值*/
//        store.setDelFlag(0);
//        /** 获取用户信息，为department 赋值，创建人信息（id,姓名)，创建时间 */
//        Store loginStore = LoginStoreUtil.getLoginStore();
//        store.setCreateUserId(loginStore.getStoreId());
//        store.setCreateUserName(loginStore.getStoreName());
//        store.setGmtCreate(new Date());
        return storeMapper.insertAdminStore(store);
   }

    /**
     * 根据类别查询同类店铺列表
     *@Autor 陆品贤
     * @param categoryId 类别id
     * @return 同类店铺类别集合
     */
    @Override
    public List<Store> selectShopByCategoryId(Long categoryId) {
        return null;
    }

    /*
     * @Description: (商家登录入驻)查询用户名
     * @Author: zhang_bowen
     * @CreateTime: 2022/12/2  10:33
 * @return: cn.niit.letuseatdemo.entity.domain.Store
     **/
    @Override
    public Store findStorePhone(String storePhone) {
        return storeMapper.findStorePhone(storePhone);
    }

    /*
      * @Description: (商家登录入驻)查找账号密码
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/2  10:33
 * @return: java.lang.String
      **/
    @Override
    public String findStorePassword(String storePhone) {
        return storeMapper.findStorePassword(storePhone);
    }

    /*
      * @Description: （管理员）审核商家入驻通过
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/8  17:07
 * @return: int
      **/
    @Override
    public int auditSuccess(Integer id) {
        return storeMapper.auditSuccess(id);
    }

    /*
      * @Description: （管理员）审核商家入驻不通过
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/8  17:10
 * @return: null
      **/
    @Override
    public int auditFail(Integer id){
        return storeMapper.auditFail(id);
    }
}